home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / unexmips.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-25  |  9.0 KB  |  310 lines

  1. /* Unexec for MIPS (including IRIS4D).
  2.    Copyright (C) 1988, 1992, 1993, 1994
  3.    Free Software Foundation, Inc.
  4.  
  5. This file is part of XEmacs.
  6.  
  7. XEmacs is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with XEmacs; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* Synched up with: FSF 19.28. */
  22.  
  23. #include <config.h>
  24. #include <sys/types.h>
  25. #include <sys/file.h>
  26. #include <sys/stat.h>
  27. #include <stdio.h>
  28. #include <varargs.h>
  29. #include <filehdr.h>
  30. #include <aouthdr.h>
  31. #include <scnhdr.h>
  32. #include <sym.h>
  33.  
  34. #if defined (IRIS_4D) || defined (sony)
  35. #include "getpagesize.h"
  36. #include <fcntl.h>
  37. #endif
  38.  
  39. static void fatal_unexec ();
  40. static void mark_x ();
  41.  
  42. #define READ(_fd, _buffer, _size, _error_message, _error_arg) \
  43.     errno = EEOF; \
  44.     if (read (_fd, _buffer, _size) != _size) \
  45.       fatal_unexec (_error_message, _error_arg);
  46.  
  47. #define WRITE(_fd, _buffer, _size, _error_message, _error_arg) \
  48.     if (write (_fd, _buffer, _size) != _size) \
  49.       fatal_unexec (_error_message, _error_arg);
  50.  
  51. #define SEEK(_fd, _position, _error_message, _error_arg) \
  52.     errno = EEOF; \
  53.     if (lseek (_fd, _position, L_SET) != _position) \
  54.       fatal_unexec (_error_message, _error_arg);
  55.  
  56. extern int errno;
  57. extern char *strerror ();
  58. #define EEOF -1
  59.  
  60. static struct scnhdr *text_section;
  61. static struct scnhdr *init_section;
  62. static struct scnhdr *finit_section;
  63. static struct scnhdr *rdata_section;
  64. static struct scnhdr *data_section;
  65. static struct scnhdr *lit8_section;
  66. static struct scnhdr *lit4_section;
  67. static struct scnhdr *sdata_section;
  68. static struct scnhdr *sbss_section;
  69. static struct scnhdr *bss_section;
  70.  
  71. struct headers {
  72.     struct filehdr fhdr;
  73.     struct aouthdr aout;
  74.     struct scnhdr section[10];
  75. };
  76.  
  77. /* Define name of label for entry point for the dumped executable.  */
  78.  
  79. #ifndef DEFAULT_ENTRY_ADDRESS
  80. #define DEFAULT_ENTRY_ADDRESS __start
  81. #endif
  82.  
  83. unexec (new_name, a_name, data_start, bss_start, entry_address)
  84.      char *new_name, *a_name;
  85.      unsigned data_start, bss_start, entry_address;
  86. {
  87.   int new, old;
  88.   int pagesize, brk;
  89.   int newsyms, symrel;
  90.   int nread;
  91.   struct headers hdr;
  92.   int i;
  93.   int vaddr, scnptr;
  94. #define BUFSIZE 8192
  95.   char buffer[BUFSIZE];
  96.  
  97.   old = open (a_name, O_RDONLY, 0);
  98.   if (old < 0) fatal_unexec ("opening %s", a_name);
  99.  
  100.   new = creat (new_name, 0666);
  101.   if (new < 0) fatal_unexec ("creating %s", new_name);
  102.  
  103.   hdr = *((struct headers *)TEXT_START);
  104. #ifdef MIPS2
  105.   if (hdr.fhdr.f_magic != MIPSELMAGIC
  106.       && hdr.fhdr.f_magic != MIPSEBMAGIC
  107.       && hdr.fhdr.f_magic != (MIPSELMAGIC | 1)
  108.       && hdr.fhdr.f_magic != (MIPSEBMAGIC | 1))
  109.     {
  110.       fprintf (stderr,
  111.           "unexec: input file magic number is %x, not %x, %x, %x or %x.\n",
  112.           hdr.fhdr.f_magic,
  113.           MIPSELMAGIC, MIPSEBMAGIC,
  114.           MIPSELMAGIC | 1, MIPSEBMAGIC | 1);
  115.       exit(1);
  116.     }
  117. #else /* not MIPS2 */
  118.   if (hdr.fhdr.f_magic != MIPSELMAGIC
  119.       && hdr.fhdr.f_magic != MIPSEBMAGIC)
  120.     {
  121.       fprintf (stderr, "unexec: input file magic number is %x, not %x or %x.\n",
  122.           hdr.fhdr.f_magic, MIPSELMAGIC, MIPSEBMAGIC);
  123.       exit (1);
  124.     }
  125. #endif /* not MIPS2 */
  126.   if (hdr.fhdr.f_opthdr != sizeof (hdr.aout))
  127.     {
  128.       fprintf (stderr, "unexec: input a.out header is %d bytes, not %d.\n",
  129.                hdr.fhdr.f_opthdr, sizeof (hdr.aout));
  130.       exit (1);
  131.     }
  132.   if (hdr.aout.magic != ZMAGIC)
  133.     {
  134.       fprintf (stderr, "unexec: input file a.out magic number is %o, not %o.\n",
  135.           hdr.aout.magic, ZMAGIC);
  136.       exit (1);
  137.     }
  138.  
  139. #define CHECK_SCNHDR(ptr, name, flags)                    \
  140.   ptr = NULL;                                \
  141.   for (i = 0; i < hdr.fhdr.f_nscns && !ptr; i++)            \
  142.     if (strcmp (hdr.section[i].s_name, name) == 0)            \
  143.       {                                    \
  144.         if (hdr.section[i].s_flags != flags)                \
  145.         fprintf (stderr, "unexec: %x flags (%x expected) in %s section.\n", \
  146.                  hdr.section[i].s_flags, flags, name);            \
  147.         ptr = hdr.section + i;                        \
  148.   }
  149.  
  150.   CHECK_SCNHDR (text_section,  _TEXT,  STYP_TEXT);
  151.   CHECK_SCNHDR (init_section,  _INIT,  STYP_INIT);
  152.   CHECK_SCNHDR (rdata_section, _RDATA, STYP_RDATA);
  153.   CHECK_SCNHDR (data_section,  _DATA,  STYP_DATA);
  154. #ifdef _LIT8
  155.   CHECK_SCNHDR (lit8_section,  _LIT8,  STYP_LIT8);
  156.   CHECK_SCNHDR (lit4_section,  _LIT4,  STYP_LIT4);
  157. #endif /* _LIT8 */
  158.   CHECK_SCNHDR (sdata_section, _SDATA, STYP_SDATA);
  159.   CHECK_SCNHDR (sbss_section,  _SBSS,  STYP_SBSS);
  160.   CHECK_SCNHDR (bss_section,   _BSS,   STYP_BSS);
  161. #if 0 /* Apparently this error check goes off on irix 3.3,
  162.      but it doesn't indicate a real problem.  */
  163.   if (i != hdr.fhdr.f_nscns)
  164.     fprintf (stderr, "unexec: %d sections found instead of %d.\n",
  165.         i, hdr.fhdr.f_nscns);
  166. #endif
  167.  
  168.   text_section->s_scnptr = 0;
  169.  
  170.   pagesize = getpagesize ();
  171.   /* Casting to int avoids compiler error on NEWS-OS 5.0.2.  */
  172.   brk = (((int) (sbrk (0))) + pagesize - 1) & (-pagesize);
  173.   hdr.aout.dsize = brk - DATA_START;
  174.   hdr.aout.bsize = 0;
  175.   if (entry_address == 0)
  176.     {
  177.       extern DEFAULT_ENTRY_ADDRESS ();
  178.       hdr.aout.entry = (unsigned)DEFAULT_ENTRY_ADDRESS;
  179.     }
  180.   else
  181.     hdr.aout.entry = entry_address;
  182.  
  183.   hdr.aout.bss_start = hdr.aout.data_start + hdr.aout.dsize;
  184.   rdata_section->s_size = data_start - DATA_START;
  185.  
  186.   /* Adjust start and virtual addresses of rdata_section, too. */
  187.   rdata_section->s_vaddr = DATA_START;
  188.   rdata_section->s_paddr = DATA_START;
  189.   rdata_section->s_scnptr = text_section->s_scnptr + hdr.aout.tsize;
  190.  
  191.   data_section->s_vaddr = data_start;
  192.   data_section->s_paddr = data_start;
  193.   data_section->s_size = brk - data_start;
  194.   data_section->s_scnptr = rdata_section->s_scnptr + rdata_section->s_size;
  195.   vaddr = data_section->s_vaddr + data_section->s_size;
  196.   scnptr = data_section->s_scnptr + data_section->s_size;
  197.   if (lit8_section != NULL)
  198.     {
  199.       lit8_section->s_vaddr = vaddr;
  200.       lit8_section->s_paddr = vaddr;
  201.       lit8_section->s_size = 0;
  202.       lit8_section->s_scnptr = scnptr;
  203.     }
  204.   if (lit4_section != NULL)
  205.     {
  206.       lit4_section->s_vaddr = vaddr;
  207.       lit4_section->s_paddr = vaddr;
  208.       lit4_section->s_size = 0;
  209.       lit4_section->s_scnptr = scnptr;
  210.     }
  211.   if (sdata_section != NULL)
  212.     {
  213.       sdata_section->s_vaddr = vaddr;
  214.       sdata_section->s_paddr = vaddr;
  215.       sdata_section->s_size = 0;
  216.       sdata_section->s_scnptr = scnptr;
  217.     }
  218.   if (sbss_section != NULL)
  219.     {
  220.       sbss_section->s_vaddr = vaddr;
  221.       sbss_section->s_paddr = vaddr;
  222.       sbss_section->s_size = 0;
  223.       sbss_section->s_scnptr = scnptr;
  224.     }
  225.   if (bss_section != NULL)
  226.     {
  227.       bss_section->s_vaddr = vaddr;
  228.       bss_section->s_paddr = vaddr;
  229.       bss_section->s_size = 0;
  230.       bss_section->s_scnptr = scnptr;
  231.     }
  232.  
  233.   WRITE (new, (void *) TEXT_START, hdr.aout.tsize,
  234.     "writing text section to %s", new_name);
  235.   WRITE (new, (void *) DATA_START, hdr.aout.dsize,
  236.     "writing data section to %s", new_name);
  237.  
  238.   SEEK (old, hdr.fhdr.f_symptr, "seeking to start of symbols in %s", a_name);
  239.   errno = EEOF;
  240.   nread = read (old, buffer, BUFSIZE);
  241.   if (nread < sizeof (HDRR)) fatal_unexec ("reading symbols from %s", a_name);
  242. #define symhdr ((pHDRR)buffer)
  243.   newsyms = hdr.aout.tsize + hdr.aout.dsize;
  244.   symrel = newsyms - hdr.fhdr.f_symptr;
  245.   hdr.fhdr.f_symptr = newsyms;
  246.   symhdr->cbLineOffset += symrel;
  247.   symhdr->cbDnOffset += symrel;
  248.   symhdr->cbPdOffset += symrel;
  249.   symhdr->cbSymOffset += symrel;
  250.   symhdr->cbOptOffset += symrel;
  251.   symhdr->cbAuxOffset += symrel;
  252.   symhdr->cbSsOffset += symrel;
  253.   symhdr->cbSsExtOffset += symrel;
  254.   symhdr->cbFdOffset += symrel;
  255.   symhdr->cbRfdOffset += symrel;
  256.   symhdr->cbExtOffset += symrel;
  257. #undef symhdr
  258.   do
  259.     {
  260.       if (write (new, buffer, nread) != nread)
  261.     fatal_unexec ("writing symbols to %s", new_name);
  262.       nread = read (old, buffer, BUFSIZE);
  263.       if (nread < 0) fatal_unexec ("reading symbols from %s", a_name);
  264. #undef BUFSIZE
  265.     } while (nread != 0);
  266.  
  267.   SEEK (new, 0, "seeking to start of header in %s", new_name);
  268.   WRITE (new, &hdr, sizeof (hdr),
  269.     "writing header of %s", new_name);
  270.  
  271.   close (old);
  272.   close (new);
  273.   mark_x (new_name);
  274. }
  275.  
  276. /*
  277.  * mark_x
  278.  *
  279.  * After successfully building the new a.out, mark it executable
  280.  */
  281.  
  282. static void
  283. mark_x (name)
  284.      char *name;
  285. {
  286.   struct stat sbuf;
  287.   int um = umask (777);
  288.   umask (um);
  289.   if (stat (name, &sbuf) < 0)
  290.     fatal_unexec ("getting protection on %s", name);
  291.   sbuf.st_mode |= 0111 & ~um;
  292.   if (chmod (name, sbuf.st_mode) < 0)
  293.     fatal_unexec ("setting protection on %s", name);
  294. }
  295.  
  296. static void
  297. fatal_unexec (s, va_alist)
  298.      va_dcl
  299. {
  300.   va_list ap;
  301.   if (errno == EEOF)
  302.     fputs ("unexec: unexpected end of file, ", stderr);
  303.   else
  304.     fprintf (stderr, "unexec: %s, ", strerror (errno));
  305.   va_start (ap);
  306.   _doprnt (s, ap, stderr);
  307.   fputs (".\n", stderr);
  308.   exit (1);
  309. }
  310.